package app.goods.info.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import app.goods.info.constants.ConstontValue;
import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;

/**
 * 读取properties文件内容
 * 
 * @author 15050109
 *
 */
public class PropertiesUtils {
	private static final char C1 = 'a';
	private static final char C2 = 'z';
	private static final char C3 = 'A';
	private static final char C4 = 'Z';
	private static final int OFFSET = 8;

	/**
	 * 根据properties文件的key值获取对应的value
	 * 
	 * @param key
	 * @return
	 */
	public static String getValueByKeyFromResources(String fileName, String key) {
		String value = "";
		try {
			Properties pps = new Properties();
			pps.load(new FileInputStream(getFilePath(fileName)));
			value = pps.getProperty(key);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return value;
	}

	/**
	 * 查询指定配置文件的所有内容
	 * 
	 * @param key
	 * @return
	 */
	public static Properties getAllKeyValueFromResources(String fileName) {
		Properties pps = new Properties();
		try {
			pps.load(FileReaderUtils.class.getClassLoader().getResourceAsStream(File.separator.concat(fileName)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pps;
	}

	/**
	 * 查询指定配置文件的所有内容
	 * 
	 * @param key
	 * @return
	 */
	public static Properties getAllKeyValue(String fileName) {
		String configFilePath = getResourcePath(fileName);
		Properties pps = new Properties();
		try {
			pps.load(new FileInputStream(new File(configFilePath)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pps;
	}

	/**
	 * 根据properties文件的key值设置对应的value
	 * 
	 * @param key
	 * @return
	 */
	public static void setValueForKey(String fileName, String key, String value) {
		String configFilePath = getFilePath(fileName);
		try {
			Properties pps = new Properties();
			pps.load(new FileInputStream(configFilePath));
			OutputStream fos = new FileOutputStream(configFilePath);
			pps.setProperty(key, value);
			pps.store(fos, "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static String getResourcePath(String fileName) {
		String resourcesPath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "main"
				+ File.separator + ConstontValue.PATH_RESOURCES;
		String configFilePath = resourcesPath + File.separator + fileName;
		return configFilePath;
	}

	/**
	 * 根据properties文件的key值删除对应的value
	 * 
	 * @param key
	 * @return
	 */
	public static void deletKeyAndValue(String fileName, String key) {
		String configFilePath = getResourcePath(fileName);
		try {
			Properties pps = new Properties();
			pps.load(new FileInputStream(configFilePath));
			OutputStream fos = new FileOutputStream(configFilePath);
			pps.remove(key);
			pps.store(fos, "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 加密
	public static String getBase64(String srcStr) {
		byte[] b = null;
		String encodeStr = null;
		try {
			b = srcStr.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (b != null) {
			encodeStr = new Base64Encoder().encode(b);
		}
		encodeStr = getString(encodeStr, OFFSET);
		return encodeStr;
	}

	// 解密
	public static String getFromBase64(String destStr) {
		byte[] b = null;
		destStr = getString(destStr, -OFFSET);
		String result = null;
		if (destStr != null) {
			Base64Decoder decoder = new Base64Decoder();
			try {
				b = decoder.decode(destStr);
				result = new String(b, "utf-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 字符串移位加密
	 * 
	 * @param str
	 * @param key
	 * @return
	 */
	public static String getString(String str, int key) {
		key %= 26;
		if (key == 0) {
			return str;
		}
		char[] chars = str.toCharArray();
		for (int i = chars.length - 1; i >= 0; i--) {
			if (chars[i] >= C3 && chars[i] <= C4) {
				chars[i] += key;
				if (chars[i] > C4) {
					chars[i] = (char) (chars[i] - C4 + C3 - 1);
				} else if (chars[i] < C3) {
					chars[i] = (char) (C4 - (C3 - chars[i]) + 1);
				}
			} else if (chars[i] >= C1 && chars[i] <= C2) {
				chars[i] += key;
				if (chars[i] > C2) {
					chars[i] = (char) (chars[i] - C2 + C1 - 1);
				} else if (chars[i] < C1) {
					chars[i] = (char) (C2 - (C1 - chars[i]) + 1);
				}
			}
		}
		return new String(chars);
	}

	private static String getFilePath(String fileName) {
		// 程序当前运行所在目录
		String currentPath = System.getProperty("user.dir");
		return currentPath.concat(File.separator).concat(fileName);
	}

	public static void main(String[] args) {
		String src = getBase64("2025-12-30");
		System.out.println("=" + src);
		String src5 = getBase64("2023-6-30");
		System.out.println("5=" + src5);
		src = getBase64("true");
		System.out.println("true=" + src);
		src = getBase64("false");
		System.out.println("false==" + src);

		System.out.println(getFromBase64("==UrIgVA0fUq0hUI=="));
		System.out.println(getValueByKeyFromResources(ConstontValue.CONFIG_FILE_NAME, "expiresDate"));

		System.out.println(getValueByKeyFromResources(ConstontValue.CONFIG_FILE_NAME, "waitTime"));
	}
}
